package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.ResourceDto;
import com.zzyl.service.ResourceService;
import com.zzyl.vo.ResourceVo;
import com.zzyl.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resource")
@Slf4j
@Api(tags = "资源管理")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @PostMapping("/list")
    @ApiOperation("资源列表")
    @ApiImplicitParam(name = "resourceDto",value = "资源的DTO实体类",dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.parentResourceNo","resourceDto.resourceType"})
    public ResponseResult<List<ResourceVo>> list(@RequestBody ResourceDto resourceDto){

        List<ResourceVo> list = resourceService.list(resourceDto);

        return ResponseResult.success(list);
    }

    @PostMapping("tree")
    @ApiOperation("资源树形")
    @ApiImplicitParam(name = "resourceDto",value = "资源的DTO实体类",dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.label"})
    public ResponseResult<TreeVo> resourceTreeVo(@RequestBody ResourceDto resourceDto){

        TreeVo treeVo = resourceService.resourceTreeVo(resourceDto);

        return ResponseResult.success(treeVo);
    }

    @PutMapping
    @ApiOperation(value = "资源添加", notes = "资源添加")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {"resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"})
    public ResponseResult createResource(@RequestBody ResourceDto resourceDto) {

        resourceService.createResource(resourceDto);

        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("资源修改")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {
              "resourceDto.id"
            , "resourceDto.dataState"
            , "resourceDto.icon"
            , "resourceDto.parentResourceNo"
            , "resourceDto.requestPath"
            , "resourceDto.resourceName"
            , "resourceDto.resourceType"
            , "resourceDto.sortNo"
            })
    public ResponseResult updateResource(@RequestBody ResourceDto resourceDto){

        resourceService.updateResource(resourceDto);

        return ResponseResult.success();
    }

    @PostMapping("/enable")
    @ApiOperation("启用与禁用")
    @ApiImplicitParam(name = "resourceDto", value = "资源DTO对象", required = true, dataType = "ResourceDto")
    @ApiOperationSupport(includeParameters = {
              "resourceDto.dataState"
            , "resourceDto.parentResourceNo"
            , "resourceDto.resourceNo"
    })
    public ResponseResult updateOfDataState(@RequestBody ResourceDto resourceDto){

        resourceService.updateOfDataState(resourceDto);

        return ResponseResult.success();
    }

    @DeleteMapping("/{menuId}")
    @ApiOperation("删除菜单")
    public ResponseResult deleteByMenuId(@PathVariable Long menuId){

        resourceService.deleteByMenuId(menuId);

        return ResponseResult.success();
    }
}
